package com.txh.blog.controller.admin;

import com.txh.blog.bean.User;
import com.txh.blog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class UserController{

    @Autowired
    private UserService userService;

    @GetMapping
    public String login(){
        return "admin/Login";
    }
    @GetMapping("/index")
    public String index(){
        return "admin/index";
    }
    @RequestMapping("/login")
    public String login(@RequestParam String username, @RequestParam String password, HttpSession session) {
        User login = userService.login(username, password);
        System.out.println(login);
        if(login!=null){
            session.setAttribute("user",login);
            return "admin/index";
        }else{
//            0表示登录失败
            session.setAttribute("user","账号密码错误！");
         return "redirect:/admin";
        }
    }
    @RequestMapping("logout")
    public String logout(HttpSession session){
        session.removeAttribute("user");
        return "redirect:/admin";
    }

    @RequestMapping("/findByUsername")
    @ResponseBody
    public User findByUsername(@RequestParam String username){
        User user = userService.findByUsername(username);
        return user;
    }
}
